/*
 * @Author: your name
 * @Date: 2020-11-23 09:34:03
 * @LastEditTime: 2020-11-23 11:58:43
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \nodeJs\app.js
 */

// 第一种方法，使用cheerioc 插件
const axios = require('axios');
const cheerio = require('cheerio');
const fs = require('fs')
const { writeFs, parseUncode, mkdir, parseStr, createFWS } = require('./base-fs');
const path = require('path');
const { log } = require('console');
const { setInterval } = require('timers');
let dataList = []
const spiderData = async (page) => {
  const httpUrl = `https://www.fabiaoqing.com/bqb/lists/page/${page}.html`
  let res = await axios.get(httpUrl)
  // console.log('页面请求成功');
  let $ = cheerio.load(res.data)
  $('#container #bqblist>a').each((i, el) => {
    // 获得单组表情的标题
    let bqbTit = parseUncode($(el).find('.header').html())
    bqbTit = parseStr(bqbTit)
    let bqbGroup = {
      bqbTit,
      imgList: []
    }
    /* dataList.push({
      bqbTit,
      imgList:[]
    }) */
    $(el).find('.bqppdiv').each((i, el) => {
      let imgName = $(el).find('img').attr('alt')

      imgName = imgName.split(' -')[0]
      imgName = parseStr(imgName)
      let imgUrl = $(el).find('img').attr('data-original')
      bqbGroup.imgList.push({ imgName, imgUrl })

    })
    dataList.push(bqbGroup)
  })

  writeFs('./bqbData.json', JSON.stringify(dataList),{flag:'w'})
  await downImg(dataList)
  // console.log('表情包下载成功');

}




const downImg = async (list) => {
  try {
    let rootPath = './img'
    await mkdir(rootPath)

    // 先创建文件夹
    list.forEach(async item => {
      let groupPath = rootPath + '/' + item.bqbTit

      // 获取后缀名

      await mkdir(groupPath)
      item.imgList.forEach(async item2 => {
        // 这种是中文名
        // let imgFlleName = item2.imgName + path.parse(item2.imgUrl).ext
        // 哈希名
        let imgFlleName = path.parse(item2.imgUrl).base
        let imgPath = groupPath + '/' + imgFlleName
        let ws = fs.createWriteStream(imgPath)
        let res = await axios.get(item2.imgUrl, { responseType: 'stream' })
        res.data.pipe(ws)
      })

    });
  } catch (error) {
    console.log('出错了', error);
  }
}


let count = 1

setInterval(() => {
  spiderData(count)
  count++
}, 5000);


